<?php

namespace app\common\command;

use app\common\model\Config;
use app\common\model\Shop;
use app\common\model\ShopMember;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Queue;

/**
 * 商家余额不足提醒
 * Created by PhpStorm.
 * User: Administrator
 * Time: 17:08

 */
class Yebz extends Command
{

    protected function configure()
    {
        $this->setName('yebz');
    }

    protected function execute(Input $input, Output $output)
    {
        $critical = Config::where(['name'=>'critical'])->value('value');//系统设置的达标次数
        if (empty($critical)){
            $critical = 0.3;
        }
        //获取状态正常的商家
        $list = Shop::where(['status'=>1,'is_del'=>0,'audit_status'=>1])->select()->toArray();
        if (empty($list)){
            $output->info("No Shop!");
            return;
        }
        foreach ($list as $v){
            if ($v['money'] < bcmul($v['fixed_amount'],$critical,1)){//余额小于单次固定充值金额的30%，即发站内信
                $member_id = ShopMember::where(['shop_id'=>$v['id'],'is_admin'=>1,'is_del'=>0])->value('member_id');
                if (empty($member_id)){
                    continue;
                }
                Queue::push(\app\job\Message::class,[
                    'member_id'=>$member_id,
                    'template_id'=>config('wechat_gzh')['gzh_template']['daichuligongdan'],
                    'data'=>[
                        'thing41'=>"余额不足，请及时申请充值",
                        'thing14'=>'系统',
                        'time15'=>date('Y-m-d H:i'),
                        'short_thing5'=>"待申请"
                    ]
                ],'message');
            }
        }
        $output->info("Successed!");
    }


}
